package button;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;

public abstract class Button {
	
	protected float sx;
	protected float sy;
	protected float ex;
	protected float ey;
	protected float width;
	protected float height;
	protected int flag;
	protected Image img;
	protected Sound snd;
	protected boolean canHover = true;
	protected boolean isHovered = false;
	protected boolean isClicked = false; 
	protected float deClickX;
	protected float hoverX;
	protected float clickX;
	protected float deActivityX;
	protected float drawX;	
	//myszka
	protected float mouseX;
	protected float mouseY;
	protected Input input;
	protected boolean isInputSet = false;
	protected int mouseButton = Input.MOUSE_LEFT_BUTTON;
	
	protected boolean isVisible = true;
	protected boolean isActivity = true;
	protected boolean wasClicked = false;
	
	protected boolean canMusicPlay = true;
	
	
	public Button(float sx, float sy, float width, float height, int flag, Image img, Sound snd)
	{
		this.sx = sx;
		this.sy = sy;
		this.width = width;
		this.height = height;
		this.ex = sx + width;
		this.ey = sy + height;
		
		this.flag = flag;
		this.img = img;
		this.snd = snd;
		
		//poczatek rysowania przycisku w zaleznosci od klikniecia/ najechania
		deClickX = 0;
		hoverX = width;
		deActivityX = 2 * width;
		clickX = 3 * width;
		drawX = deClickX;
	}
	public int getFlag()
	{
		return flag;
	}
	
	public void render(GameContainer container, Graphics g) throws SlickException
	{	
		if(isVisible)
			img.draw(sx, sy, ex, ey, drawX, 0, drawX + width, height );
	}
	
	public void hover(Input input)
	{
		if(!isVisible || !isActivity) return;
		initInput(input);
		
		mouseX = input.getMouseX();
		mouseY = input.getMouseY();
		
		if(isOn()) isHovered = true;
		else isHovered = false;
		
		//ustawienie obrazka
		if(isClicked) drawX = clickX;
		else drawX = deClickX;	
		
		if(canHover && isHovered) drawX += hoverX;
	}
	protected boolean isOn()
	{
		if(mouseX > sx && mouseX < ex && mouseY > sy && mouseY < ey ) return true;
		return false;
	}
	public void setVisible(boolean b)
	{
		isVisible = b;
	}
	public void setActivity(boolean b)
	{
		isActivity = b;
		if(!isActivity)
		{
			canHover = false;
			drawX = deActivityX;
		}
		else 
		{
			drawX = deClickX;
			canHover = true;
		}
	}
	public void setMouseButton(int x)
	{
		mouseButton = x;
	}
	public void initInput(Input input)
	{
		if(!isInputSet)
		{
			this.input = input;
			isInputSet = true;
		}
	}
	public abstract boolean click(Input input);
	public abstract void deClick();
	
	public boolean wasClicked()
	{	
		if(wasClicked)
		{
			wasClicked = false;
			return true;
		}
		return false;
	}
	
	public void setMusic(boolean b)
	{
		canMusicPlay = b;
	}

}
